/** 图片上传处理预留,有图片上传可打开此注释 */
window.pname = '测试';
var imgUrl;
var imgUrl1;
function upload(modelName) {
    //事件自动触发
    $('#uploadPhotoFile').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                	imgUrl = ds[1];
                	$("#imgUrl").val(ds[1]);
                    $("#myImageShow").attr('src', "../../" + ds[1]);
                    $("#myImageShow").attr('width', '100px');
                    $("#myImageShow").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });

    //事件自动触发
    $('#uploadPhotoFile1').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                	imgUrl1 = ds[1];
                    $("#imgUrl1").val(ds[1]);
                    $("#myImageShow1").attr('src', "../../" + ds[1]);
                    $("#myImageShow1").attr('width', '100px');
                    $("#myImageShow1").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });

    //事件自动触发
    $('#uploadPhotoFile2').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                    $("#imgUrl2").val(ds[1]);
                    $("#myImageShow2").attr('src', "../../" + ds[1]);
                    $("#myImageShow2").attr('width', '100px');
                    $("#myImageShow2").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });

    //事件自动触发
    $('#uploadPhotoFile3').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                    $("#imgUrl3").val(ds[1]);
                    $("#myImageShow3").attr('src', "../../" + ds[1]);
                    $("#myImageShow3").attr('width', '100px');
                    $("#myImageShow3").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });

    //事件自动触发
    $('#uploadPhotoFile4').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                    $("#imgUrl4").val(ds[1]);
                    $("#myImageShow4").attr('src', "../../" + ds[1]);
                    $("#myImageShow4").attr('width', '100px');
                    $("#myImageShow4").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });


    //事件自动触发
    $('#uploadPhotoFile5').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                    $("#imgUrl5").val(ds[1]);
                    $("#myImageShow5").attr('src', "../../" + ds[1]);
                    $("#myImageShow5").attr('width', '100px');
                    $("#myImageShow5").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });

    //事件自动触发
    $('#uploadPhotoFile6').fileupload({
        url: '../../cms/' + modelName + '/photoUpload',
        dataType: 'json',
        done: function (e, data) {
            console.log("fileupload1:"+data);
            data = data.result;//替换掉原来的data
            console.log("fileupload2:"+data.toString());
            if (data) {
                var ds = data.object.split(":");
                if (ds[0] == 1) {
                    $("#imgUrl6").val(ds[1]);
                    $("#myImageShow6").attr('src', "../../" + ds[1]);
                    $("#myImageShow6").attr('width', '100px');
                    $("#myImageShow6").attr('height', '100px');
                } else {
                    myAlert(ds[1]);
                }
            }
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            console.log("上传进度："+progress);
        },
        fail:function (e, data) {
            console.log("fail..." + e + data);
        },
        start:function (e) {
            console.log("start..." + e);
        }
    });
}
// 返回函数
function backAction(backurl) {

    /*if (getUrlParam('from')) {
     location.href = getUrlParam('from');
     } else {
     window.location.href = backurl;
     }*/
    javascript:history.back(-1);
}
// 通过ID获取单条数据
function getDataById(url, tmpl, content) {
    var tId = getUrlParam("id");
    if (!tId) {
        myAlert('id参数不能为空');
        return;
    }
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: {
            modelId: tId
        },
        success: function (data) {
            console.log(data);
            if (data.code == 1) {
                var contentHtml = $(tmpl).render(data.object, {
                    mydata: data
                });
                $(content).html(contentHtml);
                $("#startTime").datetimepicker({
                    minView: "month",
                    format: 'yyyy-mm-dd',
                    language: 'zh-CN',
                    autoclose: true
                }).on('changeDate', function (ev) {
                    var starttime = $("#startTime").val();
                    $("#validityTime").datetimepicker('setStartDate', starttime);
                    $("#startTime").datetimepicker('hide');
                });
                $("#validityTime").datetimepicker({
                    minView: "month",
                    format: 'yyyy-mm-dd',
                    language: 'zh-CN',
                    autoclose: true
                }).on('changeDate', function (ev) {
                    var endtime = $("#validityTime").val();
                    $("#startTime").datetimepicker('setEndDate', validityTime);
                    $("#validityTime").datetimepicker('hide');
                });
            } else {
                var contentHtml = $(tmpl).render("");
                $(content).html(contentHtml);
            }
        }
    });
}

//保存banner图数据
function saveBanner(url, backUrl) {
    var picurl = $('#picurl').val();
    var type = $('#type').val();
    var isSubmit = true;
    if (type == 1) {
        if (picurl == '') {
            isSubmit = false;
        }
    } else if (type == 5) {
        if (intro == '') {
            isSubmit = false;
        }
    }

    if (isSubmit) {
        $("#saveOrupdate").addClass('disable_btn');
        $.ajax({
            url: url,
            type: "POST",
            data: $("#submit_form").serialize(),
            success: function (data) {
                if (data.code == 1) {
                    backAction(backUrl);
                } else {
                    $("#saveOrupdate").removeClass('disable_btn');
                    myAlert(data.message || '数据加载失败，稍后重试！');
                }
            },
            error: function () {
                $("#saveOrupdate").removeClass('disable_btn');
            }
        });
    } else {
        myAlert("还有不符合规定的字段填写，请检查！");
    }

}

// 保存数据
function saveData(url, backUrl) {
    $('#submit_form').isValid(function (result, form, errors) {
        if (!result) {
            myAlert("还有不符合规定的字段填写，请检查！");
            return;
        }
        $("#saveOrupdate").addClass('disable_btn');
        $.ajax({
            url: url,
            type: "POST",
            data: $("#submit_form").serialize(),
            success: function (data) {
                if (data.code == 1) {
                    backAction(backUrl);
                } else {
                    $("#saveOrupdate").removeClass('disable_btn');
                    myAlert(data.message || '数据加载失败，稍后重试！');
                }
            },
            error: function () {
                $("#saveOrupdate").removeClass('disable_btn');
            }
        });
    });
}
//保存协议的方法
function saveConfig(url, backUrl) {
    $('#submit_form').isValid(function (result) {
        if (!result) {
            myAlert("还有不符合规定的字段填写，请检查！");
            return;
        }
        if ($("#type").val() == "") {
            myAlert("没有选择协议类型");
            return;
        }
        $("#saveOrupdate").addClass('disable_btn');
        $.ajax({
            url: url,
            type: "POST",
            data: $("#submit_form").serialize(),
            success: function (data) {
                if (data.code == 1) {
                    backAction(backUrl);
                } else {
                    $("#saveOrupdate").removeClass('disable_btn');
                    myAlert(data.message || '数据加载失败，稍后重试！');
                }
            }
        });
    });
}
//保存房间
function saveRoom(url, backUrl) {
    $('#submit_form').isValid(function (result) {
        if (!result) {
            myAlert("还有不符合规定的字段填写，请检查！");
            return;
        }
        if ($("#hotelName").val() == "") {
            myAlert("没有选择酒店");
            return;
        }
        $.ajax({
            url: url,
            type: "POST",
            data: $("#submit_form").serialize(),
            success: function (data) {
                if (data.code == 1) {
                    backAction(backUrl);
                } else {
                    myAlert(data.message || '数据加载失败，稍后重试！');
                }
            }
        });
    });
}

//保存数据
function savevaluationData(url, backUrl) {
    if ($(".select-item").length == 0) {
        myAlert("您还没有选择省份");
        return;
    }
    if (!$("#expressPrice").val() || !$("#continuedPrice").val()) {
        myAlert("还有不符合规定的字段填写，请检查！");
        return;
    }
    var city;
    if ($(".select-item").length == 2) {
        city = $(".title").children().last().text();
    } else {
        city = null;
    }
    $("#saveOrupdate").addClass('disable_btn');
    $.ajax({
        url: url,
        type: "POST",
        data: {
            expressPrice: $("#expressPrice").val(),
            continuedPrice: $("#continuedPrice").val(),
            province: $(".title").children().first().text(),
            city: city,
            id: $("#tId").val()
        },
        success: function (data) {
            if (data.code == 1) {
                backAction(backUrl);
            } else {
                $("#saveOrupdate").removeClass('disable_btn');
                myAlert(data.message || '数据加载失败，稍后重试！');
            }
        }
    });
}

//通过ID获取物流的单条数据
function getValuationDataById(url, tmpl, content) {
    var tId = getUrlParam("id");
    if (!tId) {
        myAlert('id参数不能为空');
        return;
    }
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: {
            modelId: tId
        },
        success: function (data) {
            console.log(data);
            if (data.code == 1) {
                $("#tId").val(data.object.id);
                var _html = "";
                if (data.object.province) {
                    //data.object.province;
                    _html += "<span class='select-item' data-count='province' data-code='610000'>" + data.object.province + "</span>";
                }
                if (data.object.city) {
                    //$("#s_city").val(data.object.city);
                    _html += "/<span class='select-item' data-count='city' data-code='610100'>" + data.object.city + "</span>";
                }
                console.log(_html);
                $('.title').html(_html).attr('style', 'display:inline');
                $('.title').prev().hide();
                /*$('.city-select-content').find('.province').find('a').each(function(){
                 if(data.object.province == $(this).attr('title')){
                 $(this).addClass('active');
                 CityPicker.prototype.tab();
                 }
                 });*/
                $("#expressPrice").val(data.object.expressPrice);
                $("#continuedPrice").val(data.object.continuedPrice);
                if (data.object.expressPrice) {
                    $("#expressPrice1").val(data.object.expressPrice / 100);
                }
                if (data.object.continuedPrice) {
                    $("#continuedPrice1").val(data.object.continuedPrice / 100);
                }
            } else {
            }
        }
    });
}
//通过ID获取房间的单条数据
function getRoomDataById(url, tmpl, content) {
    var tId = getUrlParam("id");
    if (!tId) {
        myAlert('id参数不能为空');
        return;
    }
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: {
            modelId: tId
        },
        success: function (data) {
            if (data.code == 1) {
                var contentHtml = $(tmpl).render(data.object, {mydata: data});
                $(content).html(contentHtml);
            } else {
                var contentHtml = $(tmpl).render("");
                $(content).html(contentHtml);
            }
            //显示酒店列表
            $.ajax({
                url: root + 'cms/hotel/findHotelNEStatusPage',
                dataType: 'json',
                type: 'POST',
                data: {},
                success: function (data1) {
                    if (data1.code == 1) {
                        var _options = "<option value=''>请选择</option>";
                        $.each(data1.object.content, function () {
                            if (data.object.hotelId == this.id) {
                                _options += "<option value='" + this.id + "' selected>" + this.hotelName + "</option>";
                            } else {
                                _options += "<option value='" + this.id + "'>" + this.hotelName + "</option>";
                            }
                        });
                        $('#hotelName').html(_options);
                    }
                    else {
                        myAlert(data1.message);
                    }
                }
            });
        }
    });
}
$(document).ready(function () {
    // 清除时间
    $("#submit_form").on('click', '.datetime_clear', function () {
        $(this).next().val('');
    });
});
